//print all permutation of a set from 0 to n-1
#include <vector>
#include <bitset>
#include <iostream>
#include <memory>

void construct_candidates(int a[], int k, int start, int n, int c[], int & ncandidates){
	std::vector<bool> in_perm(n,false);
	for (int i = 0; i < k; ++i) in_perm[ a[i]-start ]  = true;
	ncandidates = 0;
	for (int i = 0; i < n; ++i) {
		if ( in_perm[i] == false ) {
			c[ncandidates++] = i+start;
		}
	}
}

void process_solution(int a[], int k){
	for (int i=0; i<k; ++i)
		std::cout << a[i] << " ";
	std::cout << std::endl;
}

bool is_a_solution(int a[], int k, int n) {
	return k==n;
}

void backtrack(int a[], int k, int start, int n){
	std::vector<int> c(n-k);
	int ncandidates;
	if ( is_a_solution(a, k, n) )
		process_solution(a, k);
	else {
		construct_candidates(a, k, start, n, c.data(), ncandidates);
		for (int i = 0; i < ncandidates; ++i) {
			a[k] = c[i];
			backtrack(a,k+1,start, n);
		}
	}
}
//generate from 0 to n-1
void generate_permutations(int start, int n) {
	std::vector<int> a(n);
	backtrack(a.data(), 0, start, n);
}

void test_backtrack_permutation(){
	int n;
	while (std::cin >> n) {
		if (n < 0)
			break;
		generate_permutations(2, n);
	}
}